<template>
  <el-dialog v-model="dialogVisible" :title="title" width="30%" :before-close="handleClose">
    <div class="class-dialog">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px">
        <el-form-item label="银行编号" prop="bank_wseq">
          <el-input placeholder="输入银行编号" style="width: 100%" v-model="ruleForm.bank_wseq" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bank_name">
          <el-input placeholder="输入银行名称" style="width: 100%" v-model="ruleForm.bank_name" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status" :disabled="ruleForm.id ? true : false">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="背景色">
          <el-color-picker v-model="ruleForm.bank_bg" />
        </el-form-item>

        <div v-loading="loading" element-loading-text="上传中...">
          <el-form-item label="银行Logo" style="width: 100%">
            <div class="img-box" v-for="(item, i) in coverImgList" :key="i">
              <img :src="item.url" alt="" />

              <div class="occlusion">
                <el-icon class="del" @click="coverRemove(i)">
                  <IEpDelete />
                </el-icon>
              </div>
            </div>
            <el-upload action="#" list-type="picture-card" :auto-upload="false" :show-file-list="false"
              :file-list="coverImgList" :on-change="onChange">
              <el-icon>
                <IEpPlus />
              </el-icon>
            </el-upload>
          </el-form-item>
        </div>


      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/system/bank";
import { uploadImgApi } from "@/plugins/api/upload";

let ruleForm = ref({});
let rules = {
  bank_name: [{ required: true, message: "请输入银行名称", trigger: "blur" }],
  bank_wseq: [{ required: true, message: "请输入银行编号", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "blur" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  coverImgList.value = [];

  if (type == "add") {
    ruleForm.value = {};
    ruleForm.value.status = 1;
    title.value = "新增";
  } else {
    title.value = "编辑";
    ruleForm.value = Object.assign({}, row);
    if (row.bank_logo) {
      coverImgList.value = [
        {
          url: import.meta.env.VITE_APP_BASE_API + row.bank_logo,
        },
      ];
    }

  }

  dialogVisible.value = true;
}
// 上传图片（大）
let coverImgList = ref([]);
let loading = ref(false);
function onChange(file) {
  let formData = new FormData();
  formData.append("file", file.raw);
  loading.value = true;
  uploadImgApi(formData)
    .then((data) => {
      let obj = {
        url: import.meta.env.VITE_APP_BASE_API + data.file_url,
      };

      if (coverImgList.value.length > 0) {
        coverImgList.value = [];
        coverImgList.value.push(obj);
      } else {
        coverImgList.value.push(obj);
      }
      ruleForm.value.bank_logo = data.file_url;
      loading.value = false;

      //
    })
    .catch(() => {
      loading.value = false;
    });
}

function coverRemove(index) {
  coverImgList.value.splice(index, 1);
  ruleForm.value.bank_logo = "";
}




const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (!ruleForm.value.id) {
        addApi(ruleForm.value).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        saveApi(ruleForm.value).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}

onMounted(() => { });
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.class-dialog {
  :deep(.el-form-item) {
    width: 100%;
    margin-right: 0;
    padding-right: 10px;
    box-sizing: border-box;

    .el-input {
      width: 100%;
    }
  }

  .img-box {
    display: inline-block;
    margin-right: 5px;
    // margin-bottom: 10px;
    width: 148px;
    position: relative;

    img {
      width: 100%;
      height: 140px;
      object-fit: cover;
    }

    .occlusion {
      display: none;
      width: 100%;
      height: 140px;
      background: #9e9c9c75;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 99;
      text-align: center;

      .del {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        line-height: 148px;
        color: #464444;
        cursor: pointer;
        font-size: large;

        &:hover {
          color: rgb(130, 131, 133);
        }
      }
    }

    &:hover {
      .occlusion {
        display: block;
      }
    }
  }
}
</style>
